<MDXHeader
  title="Carousel"
  description="A carousel with motion and swipe built using Embla."
  docs="https://www.embla-carousel.com/get-started/solid/"
/>

<ComponentPreview name="carousel-demo" />

## About

The carousel component is built using the [Embla Carousel](https://www.embla-carousel.com/) library.

## Installation

<Tabs defaultValue="cli">

<TabsList>
  <TabsTrigger value="cli">CLI</TabsTrigger>
  <TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>

<TabsContent value="cli">

```bash
npx solidui-cli@latest add carousel
```

</TabsContent>

<TabsContent value="manual">

<Steps>

<Step>Install the following dependencies:</Step>

```bash
npm install embla-carousel-solid
```

<Step>Copy and paste the following code into your project.</Step>

<ComponentSource name="carousel" />

<Step>Update the import paths to match your project setup.</Step>

</Steps>

</TabsContent>

</Tabs>

## Usage

```tsx
import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious
} from "~/components/ui/carousel"
```

```tsx
<Carousel>
  <CarouselContent>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
  </CarouselContent>
  <CarouselPrevious />
  <CarouselNext />
</Carousel>
```

## Examples

### Sizes

To set the size of the items, you can use the `basis` utility class on the `<CarouselItem />`.

<ComponentPreview name="carousel-size-demo" />

```tsx {4-6}
// 33% of the carousel width.
<Carousel>
  <CarouselContent>
    <CarouselItem class="basis-1/3">...</CarouselItem>
    <CarouselItem class="basis-1/3">...</CarouselItem>
    <CarouselItem class="basis-1/3">...</CarouselItem>
  </CarouselContent>
</Carousel>
```

```tsx {4-6}
// 50% on small screens and 33% on larger screens.
<Carousel>
  <CarouselContent>
    <CarouselItem class="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
    <CarouselItem class="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
    <CarouselItem class="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
  </CarouselContent>
</Carousel>
```

### Spacing

To set the spacing between the items, we use a `pl-[VALUE]` utility on the `<CarouselItem />` and a negative `-ml-[VALUE]` on the `<CarouselContent />`.

<Alert class="mt-6">
  <AlertDescription>
    **Why:** I tried to use the `gap` property or a `grid` layout on the `
    <CarouselContent />` but it required a lot of math and mental effort to get the
    spacing right. I found `pl-[VALUE]` and `-ml-[VALUE]` utilities much easier to
    use.

    You can always adjust this in your own project if you need to.

  </AlertDescription>
</Alert>

<ComponentPreview name="carousel-spacing-demo" />

```tsx
<Carousel>
  <CarouselContent class="-ml-4">
    <CarouselItem class="pl-4">...</CarouselItem>
    <CarouselItem class="pl-4">...</CarouselItem>
    <CarouselItem class="pl-4">...</CarouselItem>
  </CarouselContent>
</Carousel>
```

```tsx
<Carousel>
  <CarouselContent class="-ml-2 md:-ml-4">
    <CarouselItem class="pl-2 md:pl-4">...</CarouselItem>
    <CarouselItem class="pl-2 md:pl-4">...</CarouselItem>
    <CarouselItem class="pl-2 md:pl-4">...</CarouselItem>
  </CarouselContent>
</Carousel>
```

### Orientation

Use the `orientation` prop to set the orientation of the carousel.

<ComponentPreview name="carousel-orientation-demo" />

```tsx
<Carousel orientation="vertical | horizontal">
  <CarouselContent>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
  </CarouselContent>
</Carousel>
```

## Options

You can pass options to the carousel using the `opts` prop. See the [Embla Carousel docs](https://www.embla-carousel.com/api/options/) for more information.

```tsx {2-5}
<Carousel
  opts={{
    align: "start",
    loop: true
  }}
>
  <CarouselContent>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
  </CarouselContent>
</Carousel>
```

## API

Use a state and the `setApi` props to get an instance of the carousel API.

<ComponentPreview name="carousel-api-demo" />

```tsx {1,4,22}
import type { CarouselApi } from "~/components/ui/carousel"

export function Example() {
  const [api, setApi] = createSignal<ReturnType<CarouselApi>>()
  const [current, setCurrent] = createSignal(0)
  const [count, setCount] = createSignal(0)

  createEffect(() => {
    if (!api()) {
      return
    }

    setCount(api().scrollSnapList().length)
    setCurrent(api().selectedScrollSnap() + 1)

    api().on("select", () => {
      setCurrent(api().selectedScrollSnap() + 1)
    })
  })

  return (
    <Carousel setApi={setApi}>
      <CarouselContent>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
      </CarouselContent>
    </Carousel>
  )
}
```

## Events

You can listen to events using the api instance from `setApi`.

```tsx {1,4-16}
import type { CarouselApi } from "~/components/ui/carousel"

export function Example() {
  const [api, setApi] = createSignal<ReturnType<CarouselApi>>()

  const onSelect = () => {
    // Do something on select.
  }

  createEffect(() => {
    if (!api()) {
      return
    }

    api().on("select", onSelect)
  })

  return (
    <Carousel setApi={setApi}>
      <CarouselContent>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
      </CarouselContent>
    </Carousel>
  )
}
```

See the [Embla Carousel docs](https://www.embla-carousel.com/api/events/) for more information on using events.

## Plugins

You can use the `plugins` prop to add plugins to the carousel.

```tsx {1,6-10}
import Autoplay from "embla-carousel-autoplay"

export function Example() {
  return (
    <Carousel
      plugins={[
        Autoplay({
          delay: 2000
        })
      ]}
    >
      // ...
    </Carousel>
  )
}
```

<ComponentPreview name="carousel-plugin-demo" />

See the [Embla Carousel docs](https://www.embla-carousel.com/api/plugins/) for more information on using plugins.
